using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;

namespace NAst.Common
{
    /// <summary>
    /// Check if the DNS server of any network interface
    /// match a given IP address
    /// </summary>
    class DnsServerTester : INetworkTest
    {
        IPAddress _dnsServerAddress = null;
        #region INetworkTest Members

        public void Initialize(string stateName, System.Xml.XmlElement config)
        {
            string ipadr = config.GetAttribute("address");
            if(string.IsNullOrEmpty(ipadr)) ipadr = config.GetAttribute("value");
            if (!string.IsNullOrEmpty(ipadr))
                _dnsServerAddress = IPAddress.Parse(ipadr);
        }

        public bool Test()
        {
            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (ni.OperationalStatus == OperationalStatus.Down)
                {
                    continue;
                }
                IPInterfaceProperties ipip = ni.GetIPProperties();
                if (ipip == null)
                    continue;
                foreach (IPAddress ipdns in ipip.DnsAddresses)
                {
                    if (ipdns.Equals(_dnsServerAddress))
                        return true;
                }
            }

            return false;
        }

        #endregion
    }
}
